package ContextFunctions

import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try

object context {
  type Contextual[T] = ExecutionContext ?=> T

  def asyncSum(x: Int, y: Int): Contextual[Future[Int]] =
    Future(x + y)

  def asyncMul(x: Int, y: Int)(using ctx: ExecutionContext) =
    Future(x * y)
}

object parse:
  type Parseable[T] = GivenInstances.StringParser[T] ?=> Try[T]

  def sumStrings(x: String, y: String): Parseable[Int] =
    val parser = summon[GivenInstances.StringParser[Int]]

    val tryA = parser.parse(x)
    val tryB = parser.parse(y)

    for
      a <- tryA
      b <- tryB
    yield a + b

def test: Unit =
  import ExecutionContext.Implicits.global

  context.asyncSum(3, 4).foreach(println)
  context.asyncMul(3, 4).foreach(println)

  println(parse.sumStrings("3", "4"))
  println(parse.sumStrings("3", "a"))